package com.visikard.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.item.ItemGiftKardRedeem;
import com.visikard.ui.R;
import com.visikard.ui.activity.RingUpRedeemActivity;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.RoundNumber;
import com.visikard.utility.UILImageUtils;

public class RingUpSuccessFragment extends BaseFragment implements ViewClickable {
	
	public static final String EXT_AMOUNT_REDEEMED = "Amount_Redeemed";
	public static final String EXT_MONEY_BUY = "RingUp_Money_Buy";

	private ItemGiftKardRedeem mListgiftKards;
	private double mMoneyBuy, mAmountRedeemed;
	private Context mContext;
	
	private TextView txtPuchaseSuccess;
	private TextView txtAmountKard;
	private ImageView imgGiftKard;
	private TextView txtNameKard;
	private TextView txtBalance;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mListgiftKards = (ItemGiftKardRedeem)bundle.getSerializable(RingUpRedeemFragment.EXT_ITEM_GIFT_KARD);
			mMoneyBuy = bundle.getDouble(EXT_MONEY_BUY, 0);
			mAmountRedeemed = bundle.getDouble(EXT_AMOUNT_REDEEMED, 0);
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(
				R.layout.ringup_giftkard_success, container, false);
		mContext = getActivity();
		innitControl(view);
		loadRingUpSuccess();
		return view;
	}
	
	private void innitControl(View view){
		txtPuchaseSuccess = (TextView) view.findViewById(R.id.text_ringup_sucess);
		txtAmountKard = (TextView) view.findViewById(R.id.text_ringup_amount);
		imgGiftKard = (ImageView) view.findViewById(R.id.img_success_giftkard);
		txtNameKard = (TextView) view.findViewById(R.id.ringup_success_namekard);
		txtBalance = (TextView) view.findViewById(R.id.ringup_success_kard_money);
	}
	
	private void loadRingUpSuccess() {
		String mInfo = mContext.getString(R.string.ringup_success_info);
		txtPuchaseSuccess.setText(mInfo);

		// txtAmountKard.setText("$" + String.format("%.2f", mAmount));
//		if (mListgiftKards.getIsGiftKard() == 1) {
//			txtAmountKard
//					.setText("$"
//							+ RoundNumber
//									.roundToSecondDigit(ItemAccepBuyGiftKard.mAmountRedeemed));
//		} else {
//			txtAmountKard.setText(mListgiftKards.getPurchase_value());
//		}
		
		//phuoc
		if(mAmountRedeemed > 0){
			txtAmountKard.setText("$" + String.format("%.2f", mAmountRedeemed));
			txtBalance.setText("$" + String.format("%.2f", RoundNumber.roundToSecondDigit((mMoneyBuy - mAmountRedeemed))));
		}else{
			txtAmountKard.setText("$" + String.format("%.2f", mMoneyBuy));
			txtBalance.setText("$0.00");
		}

		DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
		int mWidth = metrics.widthPixels * 3 / 5;
		int mHeight = mWidth * 2 / 3;

		RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) imgGiftKard
				.getLayoutParams();
		lyKard.width = mWidth;
		lyKard.height = mHeight;

		if (mListgiftKards.getIsGiftKard() == 1) {
//			ImageLoader.getInstance(mContext).loadImageCorner(imgGiftKard, mListgiftKards.getImgThumbnail(),
//					0, BitmapUtils.ROUND_ALL, 4, 3, R.drawable.kard_shadow_default_horizontal, 0,  0);
			UILImageUtils.loadImage(imgGiftKard, //image view
					mListgiftKards.getImgThumbnail(), //url
					0, //size of view
					BitmapUtils.ROUND_ALL, //round all 
					4, // round size
					3, //shadow
					R.drawable.kard_shadow_default_horizontal, //shadow resource 
					0, 0);
		} else {
//			ImageLoader.getInstance(mContext).loadImageCorner(imgGiftKard, mListgiftKards.getImgThumbnail(),
//					0, BitmapUtils.ROUND_NONE, 0, 3, R.drawable.kard_shadow_default_horizontal, 0,  0);
			UILImageUtils.loadImage(imgGiftKard, mListgiftKards.getImgThumbnail(), 0, BitmapUtils.ROUND_NONE, 0, 3, R.drawable.kard_shadow_default_horizontal, 0, 0);
		}

		if (mListgiftKards.getMerchantName().length() > 0) {
			txtNameKard.setVisibility(View.VISIBLE);
			txtNameKard.setText(mListgiftKards.getMerchantName());
		} else
			txtNameKard.setVisibility(View.GONE);
		
//		txtBalance
//		.setText("$"
//				+ RoundNumber
//						.roundToSecondDigit(ItemAccepBuyGiftKard.mPreviousBalance));
	}
	
	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
		case R.id.exit_btn_ringup:
		case R.id.success_ringup_code_btn_close:
			((RingUpRedeemActivity) getActivity()).finishActivity();
			break;
		default:
			break;
		}
	}

}
